﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using SARS.Biz;
using SARS.Biz.Facades;
using SARS.Entity;

namespace SARS.Tutor
{
    public partial class EditSession : Page
    {
        private readonly ICourseBO _courseBO = new CourseBO();
        private readonly IProgramBO _programmeBO = new ProgramBO();
        private readonly IRoomBO _roomBO = new RoomBO();
        private readonly ISessionBO _sessionBO = new SessionBO();
        private readonly IUserBO _userBO = new UserBO();

        private string id_param = string.Empty;
        private string program_param = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                id_param = Request["id"];
                program_param = Request["pid"];

                if (string.IsNullOrEmpty(id_param))
                {
                    Response.Redirect("AddSession.aspx");
                }

                ddlManager.DataSource = GetTutorLecture(); //_userBO.RetrieveUserByRoleId((int)RoleType.Tutor);
                ddlManager.DataBind();

                ddlRoom.DataSource = _roomBO.RetrieveAllRoom();
                ddlRoom.DataBind();


                if (!string.IsNullOrEmpty(id_param))
                {
                    SessionEntity entity = RetrieveSessionById(Convert.ToInt32(id_param));

                    if (entity != null)
                    {
                        lblProgram.Text = _programmeBO.RetrieveProgramById(Convert.ToInt32(program_param)).ProgramName;
                        lblCourse.Text = _courseBO.RetrieveCourseById(entity.CourseID).CourseInfo;
                        //UcProgramCourse1.SelectedCourseValue = entity.CourseID;
                        ddlManager.Items.FindByValue(entity.ManagerID.ToString()).Selected = true;
                        ddlRoom.Items.FindByValue(entity.RoomID.ToString()).Selected = true;
                        ddlSessionType.Items.FindByValue(entity.SessionType).Selected = true;
                        txtSessionTopic.Text = entity.SessionTopic;
                        dtpStartDateTime.Text = entity.StartDate.ToString("yyyy/MM/dd HH:mm");
                        dtpEndtDateTime.Text = entity.EndDate.ToString("yyyy/MM/dd HH:mm");
                    }
                }
            }
        }

        private List<UserEntity> GetTutorLecture()
        {
            List<UserEntity> userList = _userBO.RetrieveUserByRoleId((int) RoleType.Tutor).ToList();
            userList.AddRange(_userBO.RetrieveUserByRoleId((int) RoleType.Lecturer));

            return userList;
        }

        protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void ddlManager_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void ddlRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSessionTopic.Text) || string.IsNullOrEmpty(dtpStartDateTime.Text)
                || string.IsNullOrEmpty(dtpEndtDateTime.Text))
            {
                return;
            }

            id_param = Request["id"];

            if (!string.IsNullOrEmpty(id_param))
            {
                SessionEntity entity = RetrieveSessionById(Convert.ToInt32(id_param));

                if (entity != null)
                {
                    entity.SessionTopic = txtSessionTopic.Text;
                    entity.SessionType = ddlSessionType.SelectedValue;
                    entity.StartDate = Convert.ToDateTime(dtpStartDateTime.Text);
                    entity.EndDate = Convert.ToDateTime(dtpEndtDateTime.Text);
                    entity.ManagerID = Int32.Parse(ddlManager.SelectedValue);
                    entity.RoomID = Int32.Parse(ddlRoom.SelectedValue);

                    int result = _sessionBO.UpdateSessionById(entity);

                    if (result > 0)
                    {
                        Response.Redirect("SessionList.aspx");
                    }
                }
            }
        }

        private SessionEntity RetrieveSessionById(int id)
        {
            return _sessionBO.RetrieveSessionById(id);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("SessionList.aspx");
        }
    }
}